package com.imooc.controller;

import com.imooc.utils.RedisOperator;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: RedisController
 * @Package: com.imooc.controller
 * @Description:
 * @Date: 2021/8/18 7:57
 * @Author: sunzheng@beiming.com
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

  final static Logger logger = LoggerFactory.getLogger(RedisController.class);

  @Autowired
  private RedisTemplate redisTemplate;

  @Autowired
  private RedisOperator redisOperator;

  @GetMapping("/set")
  public Object set(String key, String value) {

//    redisTemplate.opsForValue().set(key, value);
    redisOperator.set(key, value);
    return "OK";
  }

  @GetMapping("/get")
  public String get(String key) {
//    return (String) redisTemplate.opsForValue().get(key);
    return redisOperator.get(key);
  }

  @GetMapping("/delete")
  public Object delete(String key) {

//    redisTemplate.delete(key);
    redisOperator.del(key);
    return "OK";
  }

  /**
   * 批量查询
   * @param keys
   * @return
   */
  @GetMapping("/mget")
  public Object mget(String... keys) {
    List<String> keysList = Arrays.asList(keys);
    return redisOperator.mget(keysList);
  }

}
